<?php
namespace msms\aliyun;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\AddSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\DeleteSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\ModifySmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\QuerySmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendBatchSmsRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendMessageToGlobeRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Model;
use Darabonba\OpenApi\Models\Config;
use msms\Curl;
use msms\SmsConfig;

class Alisms extends SmsConfig
{
    private $url = [
        'SendSms'=>'http://dysmsapi.aliyuncs.com/',
    ];
    protected $obj=null;
    public function __construct(array $config=[]){
        if(!empty($config)){
            $config = new Config($config);
            // 访问的域名
            $config->endpoint = "dysmsapi.aliyuncs.com";
            $this->obj = new Dysmsapi($config);
        }
    }
    public function setConfig(array $config = [])
    {
        $config = new Config($config);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        $this->obj = new Dysmsapi($config);
        return $this;
//        return parent::setConfig($config); // TODO: Change the autogenerated stub
    }

    /**发送国内短信
     * @param array模板短信需要填充 $message
     * @param array接收用户 $phone
     * @param string模板code $TemplateCode
     * @param string签名 $SignName
     * @return mixed
     */
     public function SendSms(array $message=[],array $phone=[],string $TemplateCode='',string $SignName=''){
         if(empty($phone)){
             $this->message = '请选择要发送的手机号码';
             return false;
         }
         $phone = implode(',',$phone);
         $message = json_encode($message,JSON_UNESCAPED_UNICODE);
         $query = new SendSmsRequest(['phoneNumbers'=>$phone,'templateCode'=>$TemplateCode,'templateParam'=>$message,'signName'=>$SignName]);
         $res = $this->obj->sendSms($query)->toMap();
         if(isset($res['body'])){
             if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                 return true;
             }else{
                 $this->message = $res['body']['Message'];
                 return false;
             }
         }else{
             $this->message = '请求失败';
             return false;
         }
    }
    /**发送国际短信
     * @param array模板短信需要填充 $message
     * @param array接收用户 $phone
     * @param string模板code $TemplateCode
     * @param string签名 $SignName
     * @return mixed
     */
    public function SendMessageToGlobe(array $message=[],array $phone=[],string $TemplateCode='',string $SignName=''){
        if(empty($phone)){
            $this->message = '请选择要发送的手机号码';
            return false;
        }
        $phone = implode(',',$phone);
        $message = json_encode($message,JSON_UNESCAPED_UNICODE);
        $query = new SendMessageToGlobeRequest(['phoneNumbers'=>$phone,'templateCode'=>$TemplateCode,'templateParam'=>$message,'signName'=>$SignName]);
        $res = $this->obj->sendMessageToGlobe($query)->toMap();
//        echo '<pre>';
//        print_r($res);
//        exit;
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return true;
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }

    /**群发消息
     * @param array $TemplateParamJson
     * @param array $PhoneNumberJson
     * @param string $TemplateCode
     * @param string $SignNameJson
     * @return bool
     */
    public function SendBatchSms($TemplateParamJson=[],$PhoneNumberJson=[],$TemplateCode='',$SignNameJson=[]){
        if(empty($PhoneNumberJson)){
            $this->message = '请添加要发送的手机';
            return false;
        }
        $PhoneNumberJson = json_encode($PhoneNumberJson,JSON_UNESCAPED_UNICODE);
        $TemplateParamJson = json_encode($TemplateParamJson,JSON_UNESCAPED_UNICODE);
        $SignNameJson = json_encode($SignNameJson,JSON_UNESCAPED_UNICODE);
        $query = new SendBatchSmsRequest(['phoneNumberJson'=>$PhoneNumberJson,'templateParamJson'=>$TemplateParamJson,'signNameJson'=>$SignNameJson,'templateCode'=>$TemplateCode]);
        $res = $this->obj->sendBatchSms($query)->toMap();
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return true;
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }
    /**申请添加模板
     * @param array $data
     * @return mixed
     */
    public function AddSmsTemplate($data=[]){
        $addSmsTemplateReques =  new AddSmsTemplateRequest($data);
        $res = $this->obj->addSmsTemplate($addSmsTemplateReques)->toMap();
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return $res['body'];
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }

    /**查询模板审核状态
     * @param array $data
     * @return mixed
     */
    public function QuerySmsTemplate($TemplateCode){
        $querySmsTemplateRequest = new QuerySmsTemplateRequest(['templateCode'=>$TemplateCode]);
        $res = $this->obj->querySmsTemplate($querySmsTemplateRequest)->toMap();
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return $res['body'];
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }

    /**修改未通过审核的短信模板
     * @param array $data
     * @return mixed
     */
    public function ModifySmsTemplate($data=[]){
        $query = new ModifySmsTemplateRequest($data);
        $res = $this->obj->modifySmsTemplate($query)->toMap();
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return true;
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }

    /**删除模板
     * @param string $TemplateCode
     * @return mixed
     */
    public function DeleteSmsTemplate($TemplateCode){
        $querySmsTemplateRequest = new DeleteSmsTemplateRequest(['templateCode'=>$TemplateCode]);
        $res = $this->obj->deleteSmsTemplate($querySmsTemplateRequest)->toMap();
        if(isset($res['body'])){
            if(isset($res['body']['Code'])&&$res['body']['Code']=='OK'){
                return true;
            }else{
                $this->message = $res['body']['Message'];
                return false;
            }
        }else{
            $this->message = '请求失败';
            return false;
        }
    }
}